//
//  KACouponAlertView.swift
//  kano
//
//  Created by Frank on 2024/11/11.
//

import UIKit

class KACouponAlertView: UIView {
        
//    lazy var var_backgroundView: UIVisualEffectView = {
//        let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.systemUltraThinMaterialDark)
//        let var_backgroundView = UIVisualEffectView(frame: bounds)
//        var_backgroundView.effect = blurEffect
//        var_backgroundView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
//        return var_backgroundView
//    }()
    
    lazy var var_backgroundView: UIView = {
        let var_backgroundView = UIVisualEffectView(frame: bounds)
        var_backgroundView.frame = bounds
        var_backgroundView.backgroundColor = UIColor.ka_hexColor(0x000000, alphaVaule: 0.66)
        return var_backgroundView
    }()
    
    lazy var var_contentView: UIView = {
        let var_contentView = UIView()
        var_contentView.frame = var_backgroundView.bounds
        var_contentView.backgroundColor = .clear//.color66748A
        return var_contentView
    }()
    
    lazy var var_bgImg: UIImageView = {
        let var_bgImg = UIImageView(image: UIImage(named: "coupon_bg"))
        return var_bgImg
    }()
    
    lazy var var_cornerView: UIView = {
        let var_cornerView = UIView()
        var_cornerView.backgroundColor = .colorFFEEEE
        return var_cornerView
    }()
    
    lazy var var_bgBottom: UIImageView = {
        let var_bgBottom = UIImageView(image: UIImage(named: "coupon_bottom"))
        return var_bgBottom
    }()
    
    lazy var var_titleLabel: UILabel = {
        let var_titleLabel = UILabel()
        var_titleLabel.text = ""
        var_titleLabel.textColor = .colorBF441E
        var_titleLabel.font = kBoldFontSize(16)
        var_titleLabel.numberOfLines = 0
        var_titleLabel.textAlignment = .center
        var_titleLabel.translatesAutoresizingMaskIntoConstraints = false
        return var_titleLabel
    }()
    
    lazy var var_messageLabel: UILabel = {
        let var_messageLabel = UILabel()
        var_messageLabel.text = ""
        var_messageLabel.textColor = .colorDB6D4C
        var_messageLabel.font = kBoldFontSize(12)
        var_messageLabel.textAlignment = .left
        var_messageLabel.numberOfLines = 0
        var_messageLabel.translatesAutoresizingMaskIntoConstraints = false
        return var_messageLabel
    }()
    
    lazy var var_checkBtn: UIButton = {
        let var_checkBtn = UIButton()
        var_checkBtn.setBackgroundImage(UIImage(named: "couponAlter_check"), for: .normal)
        var_checkBtn.addTarget(self, action: #selector(ka_checkBtnClick), for: .touchUpInside)
        return var_checkBtn
    }()
    
    @objc func ka_checkBtnClick() {
        guard handler != nil else {
            return
        }
        handler!(1, nil)
    }
    
    //0:close 1:check 2:use
    var handler:((Int, KAHomeCoupon?)->())? = nil
    
    
    lazy var var_closeBtn: UIButton = {
        let var_closeBtn = UIButton()
        var_closeBtn.setBackgroundImage(UIImage(named: "homeAlert_close"), for: .normal)
        var_closeBtn.addTarget(self, action: #selector(ka_closeButtonClick), for: .touchUpInside)
        return var_closeBtn
    }()
    
    @objc func ka_closeButtonClick() {
        guard handler != nil else {
            return
        }
        handler!(0, nil)
        //self.ka_dismiss()
    }
    
    var var_list: [KAHomeCoupon] = []
    
    
}

extension KACouponAlertView {
    @objc func ka_show() {
        alpha = 1.0
        let var_rootWindow : UIWindow = UIApplication.shared.keyWindow!
        var_rootWindow.addSubview(self)
        var_backgroundView.alpha = 0

        UIView.animate(withDuration: 0.25, delay: 0.0, animations: { [self] in
            var_backgroundView.alpha = 1
        })
    }
    
    @objc func ka_dismiss() {
        endEditing(true)
        UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveLinear, animations: { [self] in

        }, completion: { [self] _ in
            alpha = 0
            removeFromSuperview()
        })
    }
    
    convenience init(title: String, subTitle: String, actionTitle: String, listData: [KAHomeCoupon]) {
        self.init(frame: CGRect(x: 0, y: 0, width: var_screenWidth, height: var_screenHeight))
        var_titleLabel.text = title
        var_messageLabel.text = subTitle
        var_list = listData
        
        ka_setUI()
    }
    
    
    
    func ka_userCoupon(_ model: KAHomeCoupon) {
        if handler != nil {
            handler!(2, model)
        }
    }
    
    func ka_setUI() {
        frame = UIScreen.main.bounds
        backgroundColor = .clear

        //先按层级逐级添加视图
        addSubview(var_backgroundView)
        addSubview(var_contentView)
        var_contentView.addSubview(var_bgImg)
        var_contentView.addSubview(var_cornerView)
        var_contentView.addSubview(var_bgBottom)
        var_contentView.addSubview(var_checkBtn)
        var_contentView.addSubview(var_closeBtn)

        var_cornerView.addSubview(var_titleLabel)
        var_cornerView.addSubview(var_messageLabel)
        for (index, element) in var_list.enumerated() {
            let itemView = KACouponItem(frame: CGRectMake(kscale(12), kscale(CGFloat(117 + 80 * index)), kscale(236), kscale(80)))
            itemView.var_model = element
            itemView.handler = { model in
                self.ka_userCoupon(model)
            }
            var_cornerView.addSubview(itemView)
        }
        
        //再按视图层级逐级部署位置
        var_bgImg.snp.makeConstraints { make in
            make.width.equalTo(kscale(280))
            make.height.equalTo(kscale(235))
            make.centerX.equalTo(self)
            make.bottom.equalTo(kscale(-48))
        }
        
        var_cornerView.snp.makeConstraints { make in
            make.width.equalTo(kscale(260))
            make.height.equalTo(kscale(CGFloat(228 + 80 * var_list.count)))
            make.centerX.equalTo(self)
            make.bottom.equalTo(var_bgImg.snp.bottom)
//            make.top.equalTo(self)
        }
        var_cornerView.layer.cornerRadius = kscale(18)
        var_cornerView.layer.masksToBounds = true
        

        var_bgBottom.snp.makeConstraints { make in
            make.width.equalTo(kscale(280))
            make.height.equalTo(kscale(114))
            make.centerX.equalTo(self)
            make.bottom.equalTo(var_bgImg.snp.bottom)
        }
        
        var_checkBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(kscale(231), kscale(44)))
            make.centerX.equalTo(self)
            make.bottom.equalTo(var_bgBottom.snp.bottom).offset(kscale(-23))
        }
        
        var_closeBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(kscale(32), kscale(32)))
            make.centerX.equalTo(self)
            make.bottom.equalTo(0)
        }
        
        var_titleLabel.snp.makeConstraints { make in
            make.left.equalTo(var_cornerView).offset(kscale(12))
            make.right.equalTo(var_cornerView).offset(kscale(-12))
            make.top.equalTo(var_cornerView.snp.top).offset(kscale(20))
        }
        var_messageLabel.snp.makeConstraints { make in
            make.left.right.equalTo(var_titleLabel)
            make.top.equalTo(var_titleLabel.snp.bottom).offset(kscale(12))
        }
        
        var_contentView.snp.makeConstraints { make in
            make.width.equalTo(var_bgImg.snp.width)
            make.height.equalTo(kscale(CGFloat(276 + 80 * var_list.count)))
            make.center.equalTo(self)
        }
    }
    
}
